var up_doc = window.dialogArguments.document;

Array.prototype.indexOf = function(value) {
	for (var i = 0, l = this.length; i < l; i++)
		if (this[i] == value)
			return i;
	return -1;
};

// ////////////////////树////////////////////////////
/**
 * 潜在供应商
 */
var potentialSuppliersTree = null;// 潜在供应商
var jtgk = null;

/**
 * 生成潜在供应商供货范围树状图
 */
function createPotentialSuppliersTree() {
	potentialSuppliersTree = new dhtmlXTreeObject("potentialSuppliersTreeDIV",
			"100%", "100%", 0);
	potentialSuppliersTree.setSkin("csh_dhx_skyblue");
	potentialSuppliersTree
			.setImagePath("./js/tree/imgs/csh_dhx_skyblue/");
	potentialSuppliersTree.enableCheckBoxes(1);
	potentialSuppliersTree.enableThreeStateCheckboxes(true);
	// potentialSuppliersTree.loadXMLString(typeXML);
}

/**
 * 设置Tree的checkbox disabled
 */
function setTreeAllCheckBoxDisabled() {
	potentialSuppliersTree.deleteChildItems('0');
}

function setAllowCheckNodes( potentialSupplyRange) {

	potentialSuppliersTree.deleteChildItems('0');
	potentialSuppliersTree.loadXMLString(potentialSupplyRange);
}

/**
 * 
 * @param code
 *            供应商编码
 * @param queryType
 *            查询类型 1:仅查询供应商名称 2: 查询供应商名称和供应商基本信息
 * @param operator
 *            处理方法
 */
function _ajax(url, operator) {
	window.parent.document.getElementById("lodingDiv").style.height = window.parent.document.body.clientHeight
			- 3 + "px";
	window.parent.document.getElementById("lodingDiv").style.display = '';
	var xmlhttp = null;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				text = xmlhttp.responseText;
				if (text == 2) {
					alert("用户还未登陆，或者已掉线");
				} else if (text == 3) {
					alert("供应商编码不能为空。");
				} else {
					operator(text);
				}
			} else {
				alert("连接服务器异常");
			}
			window.parent.document.getElementById("lodingDiv").style.display = 'none';
		}
	};
	xmlhttp.open("get", url + "&temp=" + Math.random(), true);
	xmlhttp.send();
}

function queryProviderInfo(isInit) {
	var code = document.getElementById("query_providerCode").value;

	if (!code || code == "") {
		if (!isInit) {
			alert("需要查询的供应商编码不能为空");
		}
		return;
	}

	document.getElementById("providerName").value = "";
	document.getElementById("providerCode").value = "";
	setTreeAllCheckBoxDisabled();
	jtgk = null;

	_ajax(	"../../oa2uapController.do?method=getProviderInfoAndRangeXMLByCode&code="
					+ code, function(text) {
				if (text == 1) {
					alert("无法获取对应的制造部ＩＤ");
				} else {
					var result = eval("(" + text + ")");

					if (result.state) {
						if (result.state == 1) {
							alert("不存在供应商编码：" + code);
						} else if (result.state == 2) {
							alert("该供应商在当前制造部无供货范围");
						} else if (result.state == 3) {
							document.getElementById("providerName").value = result.companyName[0];
							document.getElementById("providerCode").value = code;
							setAllowCheckNodes(result.potentialSupplyRange);
							jtgk = result.jtgk;
							if (isInit) {
								var inputs = up_doc
										.getElementsByName("my:供货范围PK");

								for (var i = 0; i < inputs.length; i++) {
									potentialSuppliersTree.setCheck(
											inputs[i].value, true);
								}
							}
						} else {
							alert("数据异常，请重试");
						}
					} else {
						alert("获取数据异常");
					}
				}
			});
}

function getTRParentNode(node) {
	var tempNode = node;
	while (true) {
		if (tempNode && tempNode.tagName.toUpperCase() != "TR") {
			tempNode = tempNode.parentNode;
		} else {
			break;
		}
	}
	return tempNode.parentNode;
}

function onButtonClick() {
	up_doc.getElementById("my:供应商编码").value = document
			.getElementById("providerCode").value;
	up_doc.getElementById("my:供应商名称").value = document
			.getElementById("providerName").value;
	up_doc.getElementsByName("my:是否集团管控")[0].value = 0;
	up_doc.getElementsByName("my:集团管控")[0].value = '不在集团重点管控类别清单内';

	var trParentDom = getTRParentNode(up_doc.getElementsByName("my:供货范围1")[0]);

	var childNodesLength = trParentDom.childNodes.length;
	trParentDom.lastChild.id = 'oper';
	window.dialogArguments.addEmpty();
	while (childNodesLength > 0) {
		trParentDom.firstChild.id = 'oper';
		window.dialogArguments.del();
		trParentDom.firstChild.id = "";
		childNodesLength--;
	}

	// 潜在
	var potentialSuppliersArray = potentialSuppliersTree.getAllChecked() == ""
			? []
			: potentialSuppliersTree.getAllChecked().split(',');

	var num = potentialSuppliersArray.length;
	for (var i = 1; i < num; i++) {
		trParentDom.lastChild.id = 'oper';
		window.dialogArguments.addEmpty();
		trParentDom.lastChild.id = "";
	}
	for (var i = 0; i < potentialSuppliersArray.length; i++) {
		writeToForm(potentialSuppliersTree, potentialSuppliersArray[i], '潜在', i);

		if (jtgk[potentialSuppliersArray[i]] == "Y") {
			up_doc.getElementsByName("my:是否集团管控")[0].value = 1;
			up_doc.getElementsByName("my:集团管控")[0].value = '在集团重点管控物料类别清单内';
		}
	}

	window.close();
}

function writeToForm(tree, rangeID, type, index) {
	if (rangeID == '0' && rangeID.replace(' ', '') == '') {
		return;
	}
	
	var pRangeID = tree.getParentId(rangeID);
	if (pRangeID == '0') {
		up_doc.getElementsByName("my:供货范围1")[index].value = tree
				.getItemText(rangeID);
	} else {
		var ppRangeID = tree.getParentId(pRangeID);
		if (ppRangeID == '0') {
			up_doc.getElementsByName("my:供货范围1")[index].value = tree
					.getItemText(pRangeID);
			up_doc.getElementsByName("my:供货范围2")[index].value = tree
					.getItemText(rangeID);
		} else {
			up_doc.getElementsByName("my:供货范围1")[index].value = tree
					.getItemText(ppRangeID);
			up_doc.getElementsByName("my:供货范围2")[index].value = tree
					.getItemText(pRangeID);
			up_doc.getElementsByName("my:供货范围3")[index].value = tree
					.getItemText(rangeID);
		}
	}
	up_doc.getElementsByName("my:供货范围PK")[index].value = rangeID;
}

window.onload = function() {
	createPotentialSuppliersTree();
	document.getElementById("query_providerCode").value = up_doc
			.getElementById("my:供应商编码").value;
	queryProviderInfo(true);
};
